package com.sbj.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.sbj.dto.req.AliPayBankReq;
import com.sbj.dto.req.BankReq;
import com.sbj.dto.res.BankRes;
import com.sbj.entity.Bank;
import com.sbj.service.IBankService;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/11 21:15
 */
@RestController
@RequestMapping(value = "/bank")
@AllArgsConstructor
public class MemberBankController {

    private final IBankService bankService;

    /**
     * 添加银行卡号
     * @param bankReq
     * @return
     */
    @PostMapping(value = "/addBank")
    public Result<String> addBank(@Valid @RequestBody BankReq bankReq){
        Bank bank = Convert.convert(Bank.class, bankReq);
        bank.setMemberId(StpUtil.getLoginIdAsInt());
        bankService.saveOrUpdate(bank);
        return Result.successResult();
    }

    @GetMapping(value = "/findById/{id}")
    public Result<BankRes> findById(@PathVariable Integer id){
        Bank bank = bankService.getById(id);
        return Result.successResult(Convert.convert(BankRes.class, bank));
    }

    /**
     * 添加支付宝账号
     * @param req
     * @return
     */
    @PostMapping(value = "/addAliPayAccount")
    public Result<String> addAliPayAccount(@Valid @RequestBody AliPayBankReq req){
        Bank bank = Convert.convert(Bank.class, req);
        bank.setMemberId(StpUtil.getLoginIdAsInt());
        bank.setType(Constants.ONE);
        bankService.saveOrUpdate(bank);
        return Result.successResult();
    }



    /**
     * 查询列表
     * @return
     */
    @GetMapping(value = "/bankList")
    public Result<List<BankRes>>  bankList(){
        return Result.successResult(bankService.bankList());
    }
}
